package com.heima.admin.controller.v1;
import com.heima.admin.service.AdChannelService;


import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

@RequestMapping("/api/v1/channel")

@Api(value = "频道管理", tags = "channel", description = "频道管理API")
public class AdChannelController {
    @Autowired
    private AdChannelService channelService;
    private AdChannelController adChannelService;

//todo:=====================================================-----------------------------------------------------------
    @ApiOperation(value = "根据条件分页查询频道信息", tags = "根据条件分页查询频道信息", notes = "按照name模糊分页查询，结果按照ord升序")
    @PostMapping("/list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDTO dto) {
        return channelService.findByNameAndPage(dto);
    }

 //todo:=====================================================----------------------------------------------------------
    @ApiOperation("保存频道")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody AdChannel channel) {
        return channelService.insert(channel);
    }

    //-----------------------------------------------------------------------------------------------------------------
    @ApiOperation("频道修改")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel adChannel) {
        return channelService.update(adChannel);
    }
//todo:=====================================================-----------------------------------------------------------
//    @ApiOperation("根据频道ID删除")
//    @GetMapping("/del/{id}")
//    public ResponseResult deleteById(@PathVariable("id") Integer id) {
//
//        if(true){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
//         return channelService.deleteById(id);
//    }

    //todo:=====================================================-------------------------------------------------------
    @Transactional
    @ApiOperation("根据频道ID删除")
    @GetMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id) {

//    if(true){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
//       }
//    System.out.println(1/0);
        //任意代码手动抛出自定义异常，如下是在控制器的删除方法中抛出


        if (id == 47) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_ALLOW, "id=47不让删除");
        }
        return channelService.deleteById(id);
    }
//TODO:-----------------------------------------------------------------------------------------------
    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }

    //todo:====================================================================================================
    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(channelService.getById(id));
    }
}
